﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Visio = Microsoft.Office.Interop.Visio;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;

namespace SPNetworkTopology
{
    public partial class ThisAddIn
    {
        Initialize visOperation = null;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            // the installed version of Visio has to be 14 or > and the edition has to be PRO or >
            if (!this.IsVisio14ProInstalled)
            {
                if (Globals.ThisAddIn.Application.AlertResponse == 0)
                {
                    MessageBox.Show(string.Format(SPNetworkTopologyResource.ERR_INVALIDVISIOVER, SPNetworkTopologyResource.AppName), 
                                                  SPNetworkTopologyResource.AppName,MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return;
            }

            visOperation = new Initialize(Globals.ThisAddIn.Application);
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            if (visOperation != null)
            {
                visOperation.DeleteVisioEvents();
            }
        }

        /// <summary>
        /// Gets a value indicating whether the correct Version of Visio is installed.
        /// <para></para>
        /// Visio 2010 (14) or > and the Edition has to be PRO or >.
        /// PRO or > means that the Data features are supported.
        /// </summary>
        internal bool IsVisio14ProInstalled
        {
            get
            {
                bool retVal = false;

                // the installed version of Visio has to be 14 or > and the edition has to be PRO or >
                if (Globals.ThisAddIn.Application.TypelibMinorVersion >= 14)
                {
                    if (Globals.ThisAddIn.Application.DataFeaturesEnabled)
                    {
                        retVal = true;
                    }
                }

                return retVal;
            }
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
